package com.jiantong.admin.controller.management;

import com.alibaba.dubbo.common.json.JSON;
import com.jiantong.admin.dto.CityAreaDto;
import com.jiantong.user.model.*;
import com.jiantong.user.service.*;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Response;
import org.assertj.core.util.Strings;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * Created by ThinkPad on 2017/8/8.
 */
@Controller
@RequestMapping("/management/organization")
public class OrganizationManagementController {
    @RpcConsumer
    private AreaReadService areaReadService;
    @RpcConsumer
    private OrganizationUserReadService organizationUserReadService;

    @RpcConsumer
    private OrganizationReadService organizationReadService;
    @RpcConsumer
    private OrganizationWriteService organizationWriteService;
    @RpcConsumer
    private BreedFactoryWriteService breedFactoryWriteService;
    @RpcConsumer
    private GovernmentWriteService governmentWriteService;
    @RpcConsumer
    private DepartmentWriteService departmentWriteService;
    @RpcConsumer
    private DepartmentReadService departmentReadService;
    @RpcConsumer
    private AreaWriteService areaWriteService;
    @RpcConsumer
    private BreedFactoryReadService breedFactoryReadService;
    @RpcConsumer
    private UserProfileReadService userProfileReadService;
    @RpcConsumer
    private GovernmentReadService governmentReadService;
    @RpcConsumer
    private  GovernmentUserDutyReadService governmentUserDutyReadService;
    @RpcConsumer
    private UserReadService userReadService;

    @GetMapping("/area")
    public String area(Model model, HttpServletRequest request, HttpServletResponse response) {

        Map mapuser =  getarea(response,request);

        model.addAttribute("username",mapuser.get("username"));

        Map criteria = new HashMap();
        criteria.put("level", 2);
        criteria.put("provinceId", 31);
        List<Area> cityList = areaReadService.selectsubordinate(criteria).getResult();

        model.addAttribute("cityList", cityList);
        return "management/organization/organizationArea";
    }


    @GetMapping("/subordinate")
    @ResponseBody
    public List<Area> subordinate(@RequestParam String areaCode,
                                  @RequestParam Integer level) {
        Map criteria = new HashMap();
        criteria.put("areaCode", areaCode);
        Area area = areaReadService.selectByAreaCode(criteria).getResult();
        criteria.clear();
        switch (level) {
            case 2:
                criteria.put("level", 3);
                criteria.put("provinceId", area.getProvinceId());
                criteria.put("cityId", area.getCityId());

                break;
            case 3:
                criteria.put("level", 4);
                criteria.put("provinceId", area.getProvinceId());
                criteria.put("cityId", area.getCityId());
                criteria.put("regionId", area.getRegionId());

                break;
            case 4:
                criteria.put("level", 5);
                criteria.put("provinceId", area.getProvinceId());
                criteria.put("cityId", area.getCityId());
                criteria.put("regionId", area.getRegionId());
                criteria.put("streetId", area.getStreetId());

                break;
        }
        List<Area> resultList = areaReadService.selectsubordinate(criteria).getResult();

        return resultList;
    }

    @GetMapping("/areaAdd")
    @ResponseBody
    public Boolean areaAdd(@RequestParam String areaCode,
                           @RequestParam String name,
                           @RequestParam Integer level
    ) {
        Map criteria = new HashMap();
        criteria.put("areaCode", areaCode);
        Area area = areaReadService.selectByAreaCode(criteria).getResult();
        criteria.clear();
        switch (level) {
            case 2:
                criteria.put("level", 3);
                criteria.put("provinceId", area.getProvinceId());
                criteria.put("cityId", area.getCityId());

                break;
            case 3:
                criteria.put("level", 4);
                criteria.put("provinceId", area.getProvinceId());
                criteria.put("cityId", area.getCityId());
                criteria.put("regionId", area.getRegionId());

                break;
            case 4:
                criteria.put("level", 5);
                criteria.put("provinceId", area.getProvinceId());
                criteria.put("cityId", area.getCityId());
                criteria.put("regionId", area.getRegionId());
                criteria.put("streetId", area.getStreetId());

                break;
        }
        Map map = areaReadService.selectLastCode(criteria).getResult();
        Area area1 = new Area();
        String provinceId = map.get("provinceId").toString();
        String cityId = map.get("cityId").toString();
        String regionId = map.get("regionId").toString();
        String streetId = map.get("streetId").toString();
        String villageId = map.get("villageId").toString();
        String code = "";
        if (provinceId.length() > 1) {
            code += provinceId;
        } else {
            code += "0";
            code += provinceId;
        }
        if (cityId.length() > 1) {
            code += cityId;
        } else {
            code += "0";
            code += cityId;
        }


        switch (level) {
            case 2:
                area1.setAreaLevel(3);
                area1.setAreaName(name);
                area1.setProvinceId(Long.parseLong(map.get("provinceId").toString()));
                area1.setCityId(Long.parseLong(map.get("cityId").toString()));
                area1.setRegionId(Long.parseLong(map.get("regionId").toString()) + 1l);
                Integer r = Integer.parseInt(regionId) + 1;
                if (r > 9) {
                    code += r.toString();
                } else {
                    code += "0";
                    code += r.toString();
                }
                code += "000000";
                area1.setAreaCode(code);
                area1.setStreetId(0l);
                area1.setVillageId(0l);
                break;
            case 3:
                area1.setAreaLevel(4);
                area1.setAreaName(name);
                area1.setProvinceId(Long.parseLong(map.get("provinceId").toString()));
                area1.setCityId(Long.parseLong(map.get("cityId").toString()));
                area1.setRegionId(Long.parseLong(map.get("regionId").toString()));
                area1.setStreetId(Long.parseLong(map.get("streetId").toString()) + 1l);
                if (regionId.length() > 1) {
                    code += regionId;
                } else {
                    code += "0";
                    code += regionId;
                }
                Integer s = Integer.parseInt(streetId) + 1;
                if (s > 99) {
                    code += s.toString();
                } else if (s > 9) {
                    code += "0";
                    code += s.toString();
                } else {
                    code += "00";
                    code += s.toString();
                }

                code += "000";
                area1.setAreaCode(code);
                area1.setVillageId(0l);
                break;
            case 4:
                area1.setAreaLevel(5);
                area1.setAreaName(name);
                area1.setProvinceId(Long.parseLong(map.get("provinceId").toString()));
                area1.setCityId(Long.parseLong(map.get("cityId").toString()));
                area1.setRegionId(Long.parseLong(map.get("regionId").toString()));
                area1.setStreetId(Long.parseLong(map.get("streetId").toString()));
                area1.setVillageId(Long.parseLong(map.get("villageId").toString()) + 1l);
                if (regionId.length() > 1) {
                    code += regionId;
                } else {
                    code += "0";
                    code += regionId;
                }
                if (streetId.length() > 2) {
                    code += streetId;
                } else if (streetId.length() > 1) {
                    code += "0";
                    code += streetId;
                } else {
                    code += "00";
                    code += streetId;
                }
                Integer v = Integer.parseInt(villageId) + 1;
                if (v > 99) {
                    code += v.toString();
                } else if (v > 9) {
                    code += "0";
                    code += v.toString();
                } else {
                    code += "00";
                    code += v.toString();
                }
                area1.setAreaCode(code);
                break;
        }


        return areaWriteService.create(area1).getResult();
    }


    @GetMapping("/department")
    public String department(Model model, HttpServletRequest request, HttpServletResponse response) {
        Map mapuser =  getarea(response,request);

        model.addAttribute("username",mapuser.get("username"));
        Map criteria = new HashMap();
        criteria.put("level", 2);
        criteria.put("provinceId", 31);
        List<Area> cityList = areaReadService.selectsubordinate(criteria).getResult();

        model.addAttribute("cityList", cityList);
        return "management/organization/organizationDepartment";
    }

    @GetMapping("/organization")
    @ResponseBody
    public Map organization(@RequestParam String areaCode,
                            @RequestParam Integer level) {
        Map criteria = new HashMap();
        criteria.put("areaCode", areaCode);
        Area area = areaReadService.selectByAreaCode(criteria).getResult();
        criteria.clear();

        switch (level) {
            case 1:
                criteria.put("level", 2);
                criteria.put("provinceId", 31);
                break;

            case 2:
                criteria.put("level", 3);
                criteria.put("provinceId", 31);
                criteria.put("cityId", area.getCityId());

                break;
            case 3:
                criteria.put("level", 4);
                criteria.put("provinceId", 31);
                criteria.put("cityId", area.getCityId());
                criteria.put("regionId", area.getRegionId());

                break;
            default:
                criteria.put("level", 5);
                criteria.put("provinceId", 31);
                criteria.put("cityId", area.getCityId());
                criteria.put("regionId", area.getRegionId());
                criteria.put("streetId", area.getStreetId());
                break;

        }
        List<Area> resultList = areaReadService.selectsubordinate(criteria).getResult();

        criteria.clear();
        switch (area.getAreaLevel()) {
            case 1:
                criteria.put("provinceId", area.getProvinceId());
                break;
            case 2:
                criteria.put("provinceId", area.getProvinceId());
                criteria.put("cityId", area.getCityId());
                break;
            case 3:
                criteria.put("provinceId", area.getProvinceId());
                criteria.put("cityId", area.getCityId());
                criteria.put("regionId", area.getRegionId());
                break;
            case 4:
                criteria.put("provinceId", area.getProvinceId());
                criteria.put("cityId", area.getCityId());
                criteria.put("regionId", area.getRegionId());
                criteria.put("streetId", area.getStreetId());
                break;
            case 5:
                criteria.put("provinceId", area.getProvinceId());
                criteria.put("cityId", area.getCityId());
                criteria.put("regionId", area.getRegionId());
                criteria.put("streetId", area.getStreetId());
                criteria.put("villageId", area.getVillageId());
                break;
        }
        criteria.put("status", 1);
        Map result = new HashMap();
        List<Government> govList = governmentReadService.selectList(criteria).getResult();
        /*List<BreedFactory> breedList = (List<BreedFactory>) breedFactoryReadService.findByArea(criteria).getResult();*/
        for (Government government : govList) {
            criteria.clear();
            criteria.put("governmentId", government.getId());
            criteria.put("status", 1);
            List<Department> departments = departmentReadService.findList(criteria).getResult();
            result.put(government.getName(), departments);
        }
  /*      result.put("1", breedList);*/
        result.put("3", govList);
        result.put("area", resultList);
        return result;
    }

    @GetMapping("/showOrgan")
    @ResponseBody
    public Map showOrgan(@RequestParam Long id) {
        Map resultMap = new HashMap();
        Organization organization = organizationReadService.findById(id).getResult();
        if (organization.getType() == 1) {

            BreedFactory breedFactory = breedFactoryReadService.findById(id).getResult();
            resultMap.put("organName", breedFactory.getName());
            resultMap.put("type", "养殖场");
            User user = (User) userReadService.findById(breedFactory.getUserId()).getResult();
            resultMap.put("Name", user.getName());
            resultMap.put("Mobile", user.getMobile());
            Map criteria = new HashMap();
            criteria.put("organizationId", id);
            List<OrganizationUser> users = organizationUserReadService.selectList(criteria).getResult();
            resultMap.put("size", users.size());
        } else if (organization.getType() == 3) {
            Map criteria = new HashMap();
            criteria.put("id", id);
            Government government = governmentReadService.findById(id).getResult();
            if (government.getPid() != null) {
                Government governmentp = governmentReadService.findById(government.getPid()).getResult();
                resultMap.put("govName", governmentp.getName());
            } else {
                resultMap.put("govName", "无");
            }
            government.getName();
            resultMap.put("organName", government.getName());
            resultMap.put("type", "政府");
            User user = (User) userReadService.findById(government.getUserId()).getResult();
            resultMap.put("Name", user.getName());
            resultMap.put("Mobile", user.getMobile());
            criteria.clear();
            criteria.put("organizationId", id);
            List<OrganizationUser> users = organizationUserReadService.selectList(criteria).getResult();
            resultMap.put("size", users.size());

        }
        return resultMap;
    }

    @GetMapping("/showDepart")
    @ResponseBody
    public Map showDepart(@RequestParam Long id) {
        Map resultMap = new HashMap();
        Department department = departmentReadService.findDepartmentById(id).getResult();
        Organization organization = organizationReadService.findById(department.getGovernmentId()).getResult();
        resultMap.put("departName", department.getName());
        resultMap.put("type", department.getType());
        resultMap.put("organName", department.getGovernmentName());
        Map criteria = new HashMap();
        criteria.put("id", id);
        Government government = governmentReadService.findById(department.getGovernmentId()).getResult();
        User user = (User) userReadService.findById(government.getUserId()).getResult();
        resultMap.put("Name", user.getName());
        resultMap.put("Mobile", user.getMobile());
        criteria.clear();
        criteria.put("organizationId", id);
        List<OrganizationUser> users = organizationUserReadService.selectList(criteria).getResult();
        resultMap.put("size", users.size());


        return resultMap;
    }


    @PostMapping("/del")
    @ResponseBody
    public Boolean delOrgan(@RequestParam Long id,
                            @RequestParam Integer type) {
        if (type == 1) {
            Organization organization = organizationReadService.findById(id).getResult();
            if (organization.getType() == 1) {
                BreedFactory breedFactory = breedFactoryReadService.findById(id).getResult();
                breedFactory.setStatus(-1);
                breedFactoryWriteService.update(breedFactory);
            } else if (organization.getType() == 3) {
                Government government = governmentReadService.findById(id).getResult();
                government.setStatus(-1);
                governmentWriteService.update(government);
            }
            organization.setStatus(-1);
            return organizationWriteService.update(organization).getResult();
        } else {
            Department department = departmentReadService.findDepartmentById(id).getResult();
            department.setStatus(-1);
            return departmentWriteService.update(department).getResult();
        }
    }

    @GetMapping("/update")
    public String update() {
        return "management/organization/update";
    }


    @GetMapping("/showUpdate")
    @ResponseBody
    public Map updateOrgan(@RequestParam Long id,
                           @RequestParam Integer type) {
        Map resultMap = new HashMap();
        Map criteria = new HashMap();
        if (type == 1) {
            criteria.put("organizationType", 3);
            List<OrganizationUser> users = organizationUserReadService.selectList(criteria).getResult();
            criteria.clear();
            Organization organization = organizationReadService.findById(id).getResult();
            resultMap.put("users", users);
            resultMap.put("selectUser", organization.getUserId());
            resultMap.put("name", organization.getName());
            resultMap.put("type", 1);
        } else {
            criteria.put("status", 1);
            List<Government> governments = governmentReadService.selectList(criteria).getResult();
            resultMap.put("governments", governments);
            Department department = departmentReadService.findDepartmentById(id).getResult();
            resultMap.put("name", department.getName());
            resultMap.put("selectGov", department.getGovernmentId());
            resultMap.put("type", 3);
        }
        return resultMap;
    }

    @GetMapping("/doUpdate")
    @ResponseBody
    public Boolean doUpdate(@RequestParam Long id,
                            @RequestParam Long userId,
                            @RequestParam String name,
                            @RequestParam Integer type) {

        Boolean b = false;
        if (type == 1) {
            Organization organization = organizationReadService.findById(id).getResult();
            organization.setName(name);
            organization.setUserId(userId);
            Government government = governmentReadService.findById(id).getResult();
            government.setName(name);
            government.setUserId(userId);
            b = addOrgGov(organization, government);
        } else {
            Department department = departmentReadService.findDepartmentById(id).getResult();
            department.setName(name);
            Government government = governmentReadService.findById(userId).getResult();
            department.setGovernmentName(government.getName());
            department.setGovernmentId(government.getId());
            b = departmentWriteService.update(department).getResult();
        }

        return b;
    }

    @GetMapping("/add")
    public String add() {
        return "management/organization/add";
    }

    @GetMapping("/addGov")
    @ResponseBody
    public Map addGov(@RequestParam String code) {
        Map criteria = new HashMap();
        Map map = new HashMap();
        criteria.put("organizationType", 3);
        List<OrganizationUser> users = organizationUserReadService.selectList(criteria).getResult();
        criteria.clear();
        criteria.put("areaCode", code);
        Area area = areaReadService.selectByAreaCode(criteria).getResult();
        criteria.clear();
        String detail = "";
        if (area.getProvinceId() != 0) {
            Long pId = area.getProvinceId();
            String pIdStr = "";
            if (pId.toString().length() == 1) {
                pIdStr = "0" + pId.toString();
            } else {
                pIdStr = pId.toString();
            }
            criteria.put("areaCode", pIdStr + "0000000000");
            Area province = areaReadService.selectByAreaCode(criteria).getResult();
            detail += province.getAreaName() + " ";

            if (area.getCityId() != 0) {
                Long cId = area.getCityId();
                String cIdStr = "";
                if (cId.toString().length() == 1) {
                    cIdStr = "0" + cId.toString();
                } else {
                    cIdStr = cId.toString();
                }
                criteria.clear();
                criteria.put("areaCode", pIdStr + cIdStr + "00000000");
                Area city = areaReadService.selectByAreaCode(criteria).getResult();
                detail += city.getAreaName() + " ";

                if (area.getRegionId() != 0) {
                    Long rId = area.getRegionId();
                    String rIdStr = "";
                    if (rId.toString().length() == 1) {
                        rIdStr = "0" + rId.toString();
                    } else {
                        rIdStr = rId.toString();
                    }
                    criteria.clear();
                    criteria.put("areaCode", pIdStr + cIdStr + rIdStr + "000000");
                    Area region = areaReadService.selectByAreaCode(criteria).getResult();
                    detail += region.getAreaName() + " ";

                    if (area.getStreetId() != 0) {
                        Long sId = area.getStreetId();
                        String sIdStr = "";
                        if (sId.toString().length() == 1) {
                            sIdStr = "00" + sId.toString();
                        } else if (sId.toString().length() == 2) {
                            sIdStr = "0" + sId.toString();
                        } else {
                            sIdStr = sId.toString();

                        }
                        criteria.clear();
                        criteria.put("areaCode", pIdStr + cIdStr + rIdStr + "000");
                        Area street = areaReadService.selectByAreaCode(criteria).getResult();
                        detail += street.getAreaName() + " ";

                        if (area.getVillageId() != 0) {
                            Long vId = area.getVillageId();
                            String vIdStr = "";
                            if (vId.toString().length() == 1) {
                                vIdStr = "00" + vId.toString();
                            } else if (vId.toString().length() == 2) {
                                vIdStr = "0" + vId.toString();
                            } else {
                                vIdStr = vId.toString();

                            }
                            criteria.clear();
                            criteria.put("areaCode", pIdStr + cIdStr + rIdStr + vIdStr);
                            Area village = areaReadService.selectByAreaCode(criteria).getResult();
                            detail += village.getAreaName();
                        }
                    }
                }
            }
        }


        map.put("code", code);
        map.put("users", users);
        map.put("area", detail);
        return map;
    }

    @PostMapping("/doAdd")
    @ResponseBody
    public Boolean doAdd(@RequestParam String organizationName,
                         @RequestParam Long userId,
                         @RequestParam String areaCode) {

        Organization organization = new Organization();
        organization.setName(organizationName);
        organization.setType(3);
        organization.setStatus(1);
        organization.setUserId(userId);
        Long id = organizationWriteService.create(organization).getResult();
        Government government = new Government();
        government.setId(id);
        government.setName(organizationName);
        government.setStatus(1);
        Map criteria = new HashMap();
        criteria.put("areaCode", areaCode);
        Area area = areaReadService.selectByAreaCode(criteria).getResult();
        if (area.getProvinceId() != 0) {
            Long pId = area.getProvinceId();
            String pIdStr = "";
            if (pId.toString().length() == 1) {
                pIdStr = "0" + pId.toString();
            } else {
                pIdStr = pId.toString();
            }
            criteria.put("areaCode", pIdStr + "0000000000");
            Area province = areaReadService.selectByAreaCode(criteria).getResult();
            government.setProvinceId(pId);
            government.setProvince(province.getAreaName());
            if (area.getCityId() != 0) {
                Long cId = area.getCityId();
                String cIdStr = "";
                if (cId.toString().length() == 1) {
                    cIdStr = "0" + cId.toString();
                } else {
                    cIdStr = cId.toString();
                }
                criteria.clear();
                criteria.put("areaCode", pIdStr + cIdStr + "00000000");
                Area city = areaReadService.selectByAreaCode(criteria).getResult();
                government.setCityId(cId);
                government.setCity(city.getAreaName());
                if (area.getRegionId() != 0) {
                    Long rId = area.getRegionId();
                    String rIdStr = "";
                    if (rId.toString().length() == 1) {
                        rIdStr = "0" + rId.toString();
                    } else {
                        rIdStr = rId.toString();
                    }
                    criteria.clear();
                    criteria.put("areaCode", pIdStr + cIdStr + rIdStr + "000000");
                    Area region = areaReadService.selectByAreaCode(criteria).getResult();
                    government.setRegionId(rId);
                    government.setRegion(region.getAreaName());
                    if (area.getStreetId() != 0) {
                        Long sId = area.getStreetId();
                        String sIdStr = "";
                        if (sId.toString().length() == 1) {
                            sIdStr = "00" + sId.toString();
                        } else if (sId.toString().length() == 2) {
                            sIdStr = "0" + sId.toString();
                        } else {
                            sIdStr = sId.toString();

                        }
                        criteria.clear();
                        criteria.put("areaCode", pIdStr + cIdStr + rIdStr + "000");
                        Area street = areaReadService.selectByAreaCode(criteria).getResult();
                        government.setStreetId(sId);
                        government.setStreet(street.getAreaName());
                    } else {
                        government.setStreetId(0l);
                    }
                } else {
                    government.setRegionId(0l);
                    government.setStreetId(0l);

                }
            } else {
                government.setCityId(0l);
                government.setRegionId(0l);
                government.setStreetId(0l);
            }
        } else {
            government.setProvinceId(0l);
            government.setCityId(0l);
            government.setRegionId(0l);
            government.setStreetId(0l);
        }
        government.setUserId(userId);
        Long gId = governmentWriteService.create(government).getResult();
        Boolean b = false;
        if (gId != null) {
            b = true;
        }
        return b;
    }

    @GetMapping("/addDepart")
    public String addDepart() {
        return "management/organization/addDepart";
    }

    @GetMapping("/selectOrganization")
    @ResponseBody
    public Map selectOrganization(@RequestParam Long id) {
        Map map = new HashMap();
        Organization organization = organizationReadService.findById(id).getResult();
        map.put("organName", organization.getName());
        User user = (User) userReadService.findById(organization.getUserId()).getResult();
        map.put("user", user.getName());
        return map;
    }

    @PostMapping("/doAddDepart")
    @ResponseBody
    public Boolean doAddDepart(@RequestParam String departName,
                               @RequestParam Long governmentId,
                               @RequestParam Integer type) {
        Department department = new Department();
        department.setName(departName);
        Government government = governmentReadService.findById(governmentId).getResult();
        department.setGovernmentName(government.getName());
        department.setGovernmentId(government.getId());
        department.setStatus(1);
        department.setType(type);
        Long dId = departmentWriteService.create(department).getResult();
        Boolean b = false;
        if (dId != null) {
            b = true;
        }
        return b;
    }

    @Transactional
    private Boolean addOrgGov(Organization organization, Government government) {

        Boolean b = organizationWriteService.update(organization).getResult();
        Boolean a = governmentWriteService.update(government).getResult();
        return a && b;
    }




    public Map getarea(HttpServletResponse response, HttpServletRequest request) {
        List<String> list = new ArrayList<>();
        list.add("province");
        list.add("city");
        list.add("region");
        list.add("street");
        list.add("village");
        Cookie[] cookies = null;
        cookies = request.getCookies();
        String name = "";
        Map map = new HashMap();
        for (Cookie cookie : cookies) {

            if (cookie.getName().equals("djtid")) {
                String id = cookie.getValue();
                int Id = Integer.parseInt(id);
                Response<User> userResponse = userReadService.findById(Integer.toUnsignedLong(Id));
                name = userResponse.getResult().getName();
                map.put("username", name);
                switch (userResponse.getResult().getAreaLevel()) {
                    case 1:
                        UserProfile userProfile = userProfileReadService.findUserProfileById(userResponse.getResult().getId()).getResult();
                        String Province = userProfile.getProvince();
                        Map map6 = new HashMap();
                        map6.put("province", Province);
                        List<BreedFactory> breedFactoryList = (List<BreedFactory>) breedFactoryReadService.findByArea(map6).getResult();
                        //获取所有省下城市

                        Map map1 = new HashMap();
                        map1.put("area1", list.get(1));
                        map1.put("area2", list.get(0));
                        map1.put("area3", Province);
                        List<String> citys = breedFactoryReadService.serachAllregion1(map1);
                        //获取城市下所有区
                        List<CityAreaDto> cityAreaDtoList = new ArrayList<>();
                        for (String city : citys) {
                            CityAreaDto cityAreaDto = new CityAreaDto();
                            Map map2 = new HashMap();
                            map2.put("area1", list.get(2));
                            map2.put("area2", list.get(1));
                            map2.put("area3", city);
                            List<String> regions = breedFactoryReadService.serachAllregion1(map2);
                            cityAreaDto.setCity(city);
                            cityAreaDto.setRegions(regions);
                            cityAreaDtoList.add(cityAreaDto);

                        }

                        map.put("cityAreaDtoList", cityAreaDtoList);
                        map.put("breedFactorys", breedFactoryList);
                        //switch break;
                        break;
                    case 2:
                        UserProfile userProfile2 = userProfileReadService.findUserProfileById(userResponse.getResult().getId()).getResult();
                        String city1 = userProfile2.getCity();
                        Map map7 = new HashMap();
                        map7.put("city", city1);
                        List<BreedFactory> breedFactoryList1 = (List<BreedFactory>) breedFactoryReadService.findByArea(map7).getResult();
                        //获取所有省下城市

                        Map map21 = new HashMap();
                        map21.put("area1", list.get(2));
                        map21.put("area2", list.get(1));
                        map21.put("area3", city1);
                        List<String> citys2 = breedFactoryReadService.serachAllregion1(map21);
                        //获取城市下所有区
                        List<CityAreaDto> cityAreaDtoList1 = new ArrayList<>();
                        for (String city : citys2) {
                            CityAreaDto cityAreaDto = new CityAreaDto();
                            Map map2 = new HashMap();
                            map2.put("area1", list.get(3));
                            map2.put("area2", list.get(2));
                            map2.put("area3", city);
                            List<String> regions = breedFactoryReadService.serachAllregion1(map2);
                            cityAreaDto.setCity(city);
                            cityAreaDto.setRegions(regions);
                            cityAreaDtoList1.add(cityAreaDto);

                        }

                        map.put("cityAreaDtoList", cityAreaDtoList1);
                        map.put("breedFactorys", breedFactoryList1);
                        //switch break;
                        break;
                    case 3://区
                        UserProfile userProfile3 = userProfileReadService.findUserProfileById(userResponse.getResult().getId()).getResult();
                        String region = userProfile3.getRegion();
                        Map map36 = new HashMap();
                        map36.put("region", region);
                        List<BreedFactory> breedFactoryList3 = (List<BreedFactory>) breedFactoryReadService.findByArea(map36).getResult();
                        //获取所有区下城市镇

                        Map map31 = new HashMap();
                        map31.put("area1", list.get(3));
                        map31.put("area2", list.get(2));
                        map31.put("area3", region);
                        List<String> citys3 = breedFactoryReadService.serachAllregion1(map31);
                        //获取对应镇 对应村
                        List<CityAreaDto> cityAreaDtoList3 = new ArrayList<>();
                        for (String city : citys3) {
                            CityAreaDto cityAreaDto = new CityAreaDto();
                            Map map2 = new HashMap();
                            map2.put("area1", list.get(4));
                            map2.put("area2", list.get(3));
                            map2.put("area3", city);
                            List<String> regions = breedFactoryReadService.serachAllregion1(map2);
                            cityAreaDto.setCity(city);
                            cityAreaDto.setRegions(regions);
                            cityAreaDtoList3.add(cityAreaDto);

                        }
                        map.put("cityAreaDtoList", cityAreaDtoList3);
                        map.put("breedFactorys", breedFactoryList3);

                        //switch break;
                        break;
                    case 4://镇
                        UserProfile userProfile4 = userProfileReadService.findUserProfileById(userResponse.getResult().getId()).getResult();
                        String street = userProfile4.getStreet();
                        Map map46 = new HashMap();
                        map46.put("street", street);
                        List<BreedFactory> breedFactoryList4 = (List<BreedFactory>) breedFactoryReadService.findByArea(map46).getResult();
                        //获取所有镇下所有村

                        Map map41 = new HashMap();
                        map41.put("area1", list.get(4));
                        map41.put("area2", list.get(3));
                        map41.put("area3", street);
                        List<String> citys4 = breedFactoryReadService.serachAllregion1(map41);//所有村

                        List<CityAreaDto> cityAreaDtoList4 = new ArrayList<>();

                        CityAreaDto cityAreaDto = new CityAreaDto();

                        cityAreaDto.setCity(street);
                        cityAreaDto.setRegions(citys4);
                        cityAreaDtoList4.add(cityAreaDto);

                        map.put("cityAreaDtoList", cityAreaDtoList4);
                        map.put("breedFactorys", breedFactoryList4);
                        //switch break;
                        break;
                    case 5://村
                        UserProfile userProfile5 = userProfileReadService.findUserProfileById(userResponse.getResult().getId()).getResult();

                        List<GovernmentUserDuty> governmentUserDutyList = governmentUserDutyReadService.findGovernmentUserDutiesByUserId(userResponse.getResult().getId(), null).getResult();
                        List<Long> longlist = new ArrayList<>();
                        if (governmentUserDutyList.size()!=0){
                            for (GovernmentUserDuty governmentUserDuty : governmentUserDutyList) {
                                longlist.add(governmentUserDuty.getFactoryOrPointId());
                            }
                        }
                        List<String> villages = new ArrayList<>();
                        List<BreedFactory> breedFactoryList2 = breedFactoryReadService.findBreedFactoryDtoByIds(longlist).getResult();
                        for (BreedFactory breedFactory : breedFactoryList2) {
                            villages.add(breedFactory.getVillage());

                        }

                        List<String> listWithoutDup = new ArrayList<String>(new HashSet<String>(villages));


                        List<CityAreaDto> cityAreaDtoList5 = new ArrayList<>();

                        CityAreaDto cityAreaDto1 = new CityAreaDto();
                        cityAreaDto1.setCity(" ");

                        // cityAreaDto1.setCity(village);
                        cityAreaDto1.setRegions(listWithoutDup);

                        cityAreaDtoList5.add(cityAreaDto1);
                        map.put("userProfile", userProfile5);


                        map.put("cityAreaDtoList", cityAreaDtoList5);
                        map.put("breedFactorys", breedFactoryList2);
                        //switch break;
                        //switch break;
                        break;
                }


                break;
            }

        }

        return map;
    }


    public List findFactoryIdByArea(String region, int breedType) {

        Map map = new HashMap();
        if (breedType == -1) {
            map.put("breedType", null);
        } else {
            map.put("breedType", breedType);
        }
        List<String> list1 = new ArrayList<>();
        String[] list = region.split(",");

        for (int i = 0; i < list.length; i++) {
            list1.add(list[i]);
        }
        map.put("areas", list1);

        List<BreedFactory> breedFactoryList = (List<BreedFactory>) breedFactoryReadService.findByArea(map).getResult();

        return breedFactoryList;
    }
}
